package engineer.clientapp.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

/**
 * Servlet implementation class SendRequestServlet
 */
@WebServlet("/SendRequestServlet")
public class SendRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		ArrayList<String> ret = null;
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		System.out.println("id");
		try{
		    String endpoint = "http://localhost:8080/Server/services/MessageToDatabase";
		    Service service = new Service();
		    Call call = (Call) service.createCall();
		    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
		    call.setOperationName( new QName("http://util.com.ps", "getMessageFromDatabase") );
		    int userId=Integer.parseInt(id);
		    
		    Object tmp = call.invoke( new Object[] {userId} );
		    try {
		    	ret = (ArrayList<String>) tmp;
		    } catch(ClassCastException cce) {
		    	ret=new ArrayList<String>();
		    	ret.add((String)tmp);
		    }
		    
		    for(int i=0; i<ret.size();i++){
		    out.println(ret.get(i));
		    }
		    }
		    catch(Exception e){
		       e.printStackTrace();
		    }
	}

}
